package com.kl.airticketserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kl.airticketserver.dto.Result;
import com.kl.airticketserver.entity.SysUser;
import com.kl.airticketserver.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;

@RestController
@RequestMapping("/api/system/user")
public class SysUserController {
    
    @Autowired
    private SysUserService userService;
    
    @GetMapping("/list")
    public Result<IPage<SysUser>> list(@RequestParam(defaultValue = "1") int page,
                                       @RequestParam(defaultValue = "10") int size,
                                       @RequestParam(required = false) String username,
                                       @RequestParam(required = false) Integer status,
                                       @RequestParam(required = false) String deptId) {
        Page<SysUser> pageParam = new Page<>(page, size);
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        
        if (StringUtils.hasText(username)) {
            wrapper.like("username", username);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        if (StringUtils.hasText(deptId)) {
            wrapper.eq("dept_id", deptId);
        }
        
        IPage<SysUser> result = userService.page(pageParam, wrapper);
        return Result.success(result);
    }
    
    @PostMapping
    public Result<Void> save(@RequestBody SysUser user) {
        userService.save(user);
        return Result.success(null);
    }
    
    @PutMapping
    public Result<Void> update(@RequestBody SysUser user) {
        userService.updateById(user);
        return Result.success(null);
    }
    
    @DeleteMapping
    public Result<Void> delete(@RequestBody java.util.List<String> ids) {
        userService.removeByIds(ids);
        return Result.success(null);
    }
}